package com.pengtu.web.project.org;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.project.OwnerOrganization;
import com.pengtu.service.project.SignOrganizationManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 单位管理
 * 
 * @author libitum
 * 
 */
@Namespace("/project/org")
@Results({ @Result(name = BaseActionSupport.RELOAD, type = "redirect", location = "organization.action") })
public class OwnerorgAction extends BaseActionSupport<OwnerOrganization> {

	private static final long serialVersionUID = 1L;

	private SignOrganizationManager signOrgManager;

	// -- 页面属性 --//
	private String id;
	private OwnerOrganization ownerOrg;
	private Page<OwnerOrganization> page = new Page<OwnerOrganization>(10);// 每页5条记录

	@Autowired
	public void setSignOrgManager(SignOrganizationManager signOrgManager) {
		this.signOrgManager = signOrgManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Page<OwnerOrganization> getPage() {
		return page;
	}

	public void setPage(Page<OwnerOrganization> page) {
		this.page = page;
	}

	@Override
	public OwnerOrganization getModel() {
		return ownerOrg;
	}
	
	@Action("/quality/org/ownerorg")
	public String list() throws Exception {
		QueryFilter queryfilters = QueryFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = signOrgManager.findPageOwner(page,queryfilters);

		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		signOrgManager.saveOwner(ownerOrg);
		String mes = "";
		if(StringUtils.isNotEmpty(id)){
			mes = "修改";
		}else{
			mes = "新增";
		}
		addActionMessage(mes+"建设单位"+ownerOrg.getOrgName()+"成功");
		return input();
	}

	/**
	 * 验证机构代码是否重复
	 */
	public void checkOrgNum() {
		// TODO Auto-generated method stub
		String orgNum = Struts2Utils.getParameter("orgNum");
		String oldOrgNum = Struts2Utils.getParameter("oldOrgNum");
		if (signOrgManager.isOrgNumUnique(orgNum, oldOrgNum)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
	}

	/**
	 * 验证单位名是否重复
	 */
	public void checkOrgName() {
		// TODO Auto-generated method stub
		String orgName = Struts2Utils.getParameter("orgName");
		String oldOrgName = Struts2Utils.getParameter("oldOrgName");
		if (signOrgManager.isOrgNameUnique(orgName, oldOrgName)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		addActionMessage("删除建设单位" + ownerOrg.getOrgName());
		signOrgManager.deleteOwner(id);
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			ownerOrg = signOrgManager.getOwner("id", id);
		} else {
			ownerOrg = new OwnerOrganization();
		}
	}

}
